package com.hy.erp.inventory.service;

import java.util.Collection ;
import java.util.List ;

import javax.ejb.Stateless ;

import org.jboss.seam.ScopeType ;
import org.jboss.seam.annotations.Name ;
import org.jboss.seam.annotations.Scope ;

import com.hy.enterprise.architecture.foundation.ArchitectureFoundationException;
import com.hy.enterprise.framework.service.business.AbstractBusinessService;
import com.hy.erp.inventory.dao.interfaces.IAddressDao;
import com.hy.erp.inventory.pojo.Address;
import com.hy.erp.inventory.pojo.interfaces.IAddress;
import com.hy.erp.inventory.service.interfaces.IAddressService;
/**
 * 
 * <ul>
 * <li>开发作者：李冰</li>
 * <li>设计日期：2010-10-13；时间：上午09:16:44</li>
 * <li>类型名称：AddressService</li>
 * <li>设计目的：</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
@ Name ( "addressService" )
@ Scope ( ScopeType.STATELESS )
@ Stateless
public class AddressService extends AbstractBusinessService < IAddress > implements IAddressService
{

	@ Override
	public boolean addAddress (
								IAddress address )
	{

		if ( null == address)
		{
			return true ;
		}
		
		else
		{
			System.out
						.println ("ssssssssssssssssssssssffffffffffffffffhghhhhhhhhhhhhhhhhhhhhhh") ;


			IAddressDao addressDao =
			                       ( IAddressDao ) this
			                                          .getDao ( IAddress.class ) ;
			
		
			return addressDao
			                .persist ( address ) ;
		}
		
	
	}

	@ Override
	public boolean modifyAddress (
									Address address )
	{



		if ( ( null == address )
		     || ( null == address
		                                .getIdentifier ( ) )
		     || ( address
		                        .getIdentifier ( )
		                        .trim ( )
		                        .length ( ) == 0 ) )
		{
			return true ;
		}
		else
		{

			IAddressDao addressDao =
			                       ( IAddressDao ) this
			                                          .getDao ( IAddress.class ) ;
			
				
			if ( null == address )
			{
				throw new ArchitectureFoundationException  (
				                                            "修改实体数据时发生错误，所需要修改的实体在数据库中并不存在" ) ;
			}
			Collection<String>addressMaps=	address.getPropertyNames ( );
			String id=address.getIdentifier ( ) ;
			Address address2 =( Address ) addressDao.findByIdentifier ( id );
			
			for(String mapkey :addressMaps)
			{
				address2.set ( mapkey , address.get ( mapkey ) ); 
				
			}
			IAddress iaddress=address2;
			return ( addressDao
	                  .merge ( iaddress ) != null ) ;
			
		}
	
	
	
	}

	@ Override
	public Integer removeAddress (
									String [ ] addressIds )
	{

		if ( ( null == addressIds )
			     || ( addressIds.length == 0 ) )
		{
			return new Integer (
			                     0 ) ;
		}
		else
		{System.out
					.println (addressIds.length+"HHHHHHHHHHHHHH") ;
			

		IAddressDao addressDao =
            ( IAddressDao ) this
                               .getDao ( IAddress.class ) ;
			return addressDao
			                .remove ( addressIds ) ;
		}
		
	
	}

	@ Override
	public List < IAddress > getAllAddress ( )
	{
		IAddressDao addressDao =
            ( IAddressDao ) this
                               .getDao ( IAddress.class ) ;
			return addressDao
			                .find ( )  ;
	}

	@ Override
	public String [ ] addAddress (
									Object [ ] address )
	{
		if ( ( null == address )
			     || ( address.length == 0 ) )
		{
			return null;
		}
		else
		{

		IAddressDao addressDao =
           ( IAddressDao ) this
                              .getDao ( IAddress.class ) ;
		Address[] addresss=new Address[address.length];
		for(int i=0;i<address.length;i++)
		{
			Address address2=( Address ) address[i];
			addresss[i]=address2;
		}
		
		return addressDao
		                .persist ( addresss ) ;
	
		}
		
	}

	@ Override
	public String [ ] modifyAddress (
										Object [ ] address )
	{

		if ( ( null == address )
			     || ( address.length == 0 ) )
		{
			return null;
		}
		else
		{

		IAddressDao addressDao =
           ( IAddressDao ) this
                              .getDao ( IAddress.class ) ;
		Address[] addresss=new Address[address.length];
		for(int i=0;i<address.length;i++)
		{
			Address address2=( Address ) address[i];
			addresss[i]=address2;
		}
		
		return addressDao
		                .merge  ( addresss ) ;
	
		}
		
	
	}

}
